﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Scan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            string ips = textBox1.Text;
            
            if (ips.Length != 0)
            {
                string[] items = ips.Split('.');
                if (items.Count() != 4)
                {
                    MessageBox.Show("IP格式不对，参考：10.1-10.1.20-255");
                    return;
                }
                List<int> A = new List<int>();
                List<int> B = new List<int>();
                List<int> C = new List<int>();
                List<int> D = new List<int>();
         
                if (items[0].Contains("-"))
                {
                    string[] tmp = items[0].Split('-');
                    if (tmp.Length != 2)
                    {
                        MessageBox.Show("A-范围不全");
                        return;
                    }
                    for (int x=int.Parse(tmp[0]);x<=int.Parse(tmp[1]);x++)
                    {
                        A.Add(x);
                    }
                } else
                {
                    A.Add(int.Parse(items[0]));
                }

                if (items[1].Contains("-"))
                {
                    string[] tmp = items[1].Split('-');
                    if (tmp.Length != 2)
                    {
                        MessageBox.Show("B-范围不全");
                        return;
                    }
                    for (int y = int.Parse(tmp[0]); y <= int.Parse(tmp[1]); y++)
                    {
                        B.Add(y);
                    }
                }
                else
                {
                    B.Add(int.Parse(items[1]));
                }

                if (items[2].Contains("-"))
                {
                    string[] tmp = items[2].Split('-');
                    if (tmp.Length != 2)
                    {
                        MessageBox.Show("C-范围不全");
                        return;
                    }
                    for (int x = int.Parse(tmp[0]); x <= int.Parse(tmp[1]); x++)
                    {
                        C.Add(x);
                    }
                }
                else
                {
                    C.Add(int.Parse(items[2]));
                }

                if (items[3].Contains("-"))
                {
                    string[] tmp = items[3].Split('-');
                    if (tmp.Length != 2)
                    {
                        MessageBox.Show("D-范围不全");
                        return;
                    }
                    for (int x = int.Parse(tmp[0]); x <= int.Parse(tmp[1]); x++)
                    {
                        D.Add(x);
                    }
                }
                else
                {
                    D.Add(int.Parse(items[3]));
                }

                foreach (int x in A)
                {
                    foreach (int y in B)
                    {
                        foreach (int z in C)
                        {
                            foreach (int w in D)
                            {
                                string ip = x + "." + y + "." + z + "." + w;
                                PingIP(ip);
                            }
                        }
                    }
                }

                MessageBox.Show("扫描完毕");
            } else
            {
                MessageBox.Show("请输入IP范围");
            }
            
        }

        private void PingIP(string ip)
        {
            Ping ping = new Ping();
            ping.PingCompleted += new PingCompletedEventHandler(togrid);
            ping.SendAsync(ip, 2000, null);
        }

        private void togrid(object sender,PingCompletedEventArgs e)
        {
            
            if (e.Reply.Status == IPStatus.Success)
            {
                int index = this.dataGridView1.Rows.Add();
                this.dataGridView1.Rows[index].Cells[0].Value = index;
                this.dataGridView1.Rows[index].Cells[1].Value = e.Reply.Address.ToString();
                this.dataGridView1.Rows[index].Cells[2].Value = e.Reply.RoundtripTime.ToString();
            }
        }

        private void PortScan(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                Form2 f = new Form2(dataGridView1.CurrentCell.Value.ToString());
                f.Show();
            }
        }
    }
}
